<template>
    <div ref="editRef">
        <webtopo-svg-edit
            @on-return="onReturn"
            @on-preview="onPreview"
            @on-save="onSave"
            :data-model="data_model"
            :dataModelId="dataModelId"
        ></webtopo-svg-edit>
    </div>
</template>
<script setup>
import { onBeforeMount, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { applicationServe } from '@/assets/api/api'
import { showMsg } from '@/assets/js/utils'
const data_model = ref(localStorage.getItem('data_model') ?? null)
const route = useRoute()
const dataModelId = ref(route.query.data_model_id)
const editRef = ref(null)
data_model.value = localStorage.getItem('data_model')
    ? localStorage.getItem('data_model')
    : ''
onBeforeMount(() => {
    // showLoading()
})
onMounted(() => {
    // hideLoading()
})
import { useRouter } from 'vue-router'
const router = useRouter()
const onReturn = () => {
    router.go(-1)
}
const onPreview = (data_model) => {
    const { href } = router.resolve({
        name: 'preview',
        params: {
            data_model: JSON.stringify(data_model),
        },
    })
    window.open(href, '_blank')
    localStorage.setItem('data_model', JSON.stringify(data_model))
}
const dataJson = ref()
const onSave = (data_model) => {
    dataJson.value = {
        ...data_model,
        application_id: dataModelId.value,
    }
    applicationServe(dataJson.value)
        .then(() => showMsg('保存成功'))
        .catch(() => showMsg('保存失败', 'error'))
}
</script>
<style lang="scss"></style>
